using System;
using Beetle;

internal abstract class al : Package
{
	private new ReadObjectInfo m_a = new ReadObjectInfo();

	private new int b;

	private byte[] c = new byte[4];

	private int d;

	private int e;

	protected WriteBlock f = new WriteBlock();

	public al()
	{
	}

	public al(aq A_0)
		: base(A_0)
	{
	}

	protected abstract IMessage a(IDataReader A_0, ReadObjectInfo A_1);

	protected abstract void a(IMessage A_0, IDataWriter A_1);

	public override void Import(byte[] data, int start, int count)
	{
		e = count;
		while (e > 0)
		{
			if (b == 0)
			{
				if (e < 4)
				{
					d = e;
					Buffer.BlockCopy(data, start, c, 0, e);
					e = 0;
					break;
				}
				Writer.Reset();
				if (d == 0)
				{
					b = BitConverter.ToInt32(data, start);
					if (b > TcpUtils.DataPacketMaxLength || b <= 0)
					{
						throw NetTcpException.DataOverflow();
					}
					Writer.Write(data, start, 4);
					start += 4;
					b -= 4;
					e -= 4;
				}
				else
				{
					int num = 4 - d;
					Buffer.BlockCopy(data, start, c, d, num);
					d = 0;
					b = BitConverter.ToInt32(c, 0);
					if (b > TcpUtils.DataPacketMaxLength || b <= 0)
					{
						throw NetTcpException.DataOverflow();
					}
					Writer.Write(c, 0, 4);
					start += num;
					b -= 4;
					e -= num;
				}
			}
			if (e >= b)
			{
				Writer.Write(data, start, b);
				OnMessageDataReader(Writer);
				start += b;
				e -= b;
				b = 0;
			}
			else
			{
				Writer.Write(data, start, e);
				b -= e;
				e = 0;
			}
		}
	}

	public override void MessageWrite(IMessage msg, IDataWriter writer)
	{
		_ = writer.Length;
		WriteBlock writeBlock = ((x)writer).WriteResultBlock(new byte[4], 0, 4, f);
		a(msg, writer);
		msg.Save(writer);
		writeBlock.SetData(new byte[4], 0, 4);
	}

	public override IMessage MessageRead(IDataReader reader)
	{
		IMessage message = null;
		message = a(reader, this.m_a);
		if (message == null)
		{
			throw NetTcpException.TypeNotFound(this.m_a.ToString());
		}
		try
		{
			message.Load(reader);
			return message;
		}
		catch (Exception innerexception)
		{
			NetTcpException ex = NetTcpException.ObjectLoadError(this.m_a.ToString(), innerexception);
			throw ex;
		}
	}

	protected override void OnDisposed()
	{
		base.OnDisposed();
	}
}
